package edu.uestc.canary.admin.config;

import edu.uestc.canary.admin.web.interceptor.LoginInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource private LoginInterceptor loginInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*") //允许跨域的请求的url
                .allowedMethods("GET","POST","PUT","DELETE","PATCH","OPTIONS") //允许跨域的请求方法
                .allowedHeaders("*")//请求头
                .exposedHeaders("Authorization") //预检请求头
                //.allowCredentials(true) //是否允许携带跨域cookie凭证
                .maxAge(3600); //在3600秒内不预检
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/shop-types/**");
    }
}
